<?php namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * App\Files
 *
 * @property int $id 文件id
 * @property string $file_url 文件地址
 * @property string|null $file_name 文件名
 * @property string|null $file_ext 文件扩展名
 * @property string|null $file_type 文件类型
 * @property string|null $file_md5 文件md5
 * @property int $file_size 文件大小
 * @property string|null $deleted_at
 * @property \Carbon\Carbon|null $created_at
 * @property \Carbon\Carbon|null $updated_at
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Images[] $images
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Query\Builder|\App\Files onlyTrashed()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Files whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Files whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Files whereFileExt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Files whereFileMd5($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Files whereFileName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Files whereFileSize($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Files whereFileType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Files whereFileUrl($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Files whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Files whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Files withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\Files withoutTrashed()
 * @mixin \Eloquent
 */
class Files extends Model
{
    protected $table = 'files';
    use SoftDeletes;

    public function images(){
        return $this->hasMany('App\Images','file_id');
    }

    public function isFileExists()
    {
        if ($this->file_url.'/'.$this->file_md5 && \Storage::disk()->exists($this->file_url.'/'.$this->file_md5))
            return true;
        return false;
    }

    public function getLocalPath(){
        return storage_path().'/app/'.$this->file_url.'/'.$this->file_md5;
    }

    public function getContent(){
        if($this->isFileExists())
            return \Storage::disk()->get($this->file_url.'/'.$this->file_md5);
        return '';
    }
}